package org.yty.travelagencyredis.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.yty.travelagencyredis.Dao.ScenicSpotDao;
import org.yty.travelagencyredis.model.Entity.ScenicSpot;
import org.yty.travelagencyredis.service.ScenicSpotService;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ScenicSpotServiceImpl implements ScenicSpotService {
    private final ScenicSpotDao scenicSpotDao;

    @Override
    public List<ScenicSpot> list() {
        return this.scenicSpotDao.selectAll();
    }

    @Override
    public ScenicSpot getById(Long id) {
        return this.scenicSpotDao.selectById(id);
    }

    @Override
    public boolean update(ScenicSpot scenicSpot) {
        scenicSpot.setUpdateTime(LocalDateTime.now());
        return this.scenicSpotDao.updateById(scenicSpot);
    }

    @Override
    public boolean save(ScenicSpot scenicSpot) {
        if (scenicSpot.getId() == null){
            scenicSpot.setId(IdUtil.getSnowflakeNextId());
        }
        scenicSpot.setCreateTime(LocalDateTime.now());
        scenicSpot.setUpdateTime(LocalDateTime.now());
        return this.scenicSpotDao.insert(scenicSpot);
    }

    @Override
    public void removeById(Long id) {
        this.scenicSpotDao.deleteById(id);
    }
}
